/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using System.Globalization;
using System.Qizx.Api.Util;
using EvaluationException = System.Qizx.Api.EvaluationException;
using XQDateTime = System.Qizx.Api.Util.XQDateTime;
using XQDateTimeBase = System.Qizx.Api.Util.XQDateTimeBase;
using EvalContext = System.Qizx.XQuery.EvalContext;
using Focus = System.Qizx.XQuery.Focus;
using XQItem = System.Qizx.XQuery.XQItem;
using XQTypeException = System.Qizx.XQuery.XQTypeException;
using XQValue = System.Qizx.XQuery.XQValue;
using Expression = System.Qizx.XQuery.Op.Expression;
namespace System.Qizx.XQuery.Dt
{
	
	public class MomentType:AtomicType
	{
		public override string ShortName
		{
			get
			{
				return "moment";
			}
			
		}
		
		public override XQValue convertFromObject(System.Object obj)
		{
			if (obj is System.Globalization.XQCalendar)
			{
                obj = (obj as XQCalendar).GetDateTime();
			}
			if (obj is System.DateTime)
			{
				System.DateTime date = (System.DateTime) obj;
                return new SingleMoment(XQDateTime.fromDate(ref date, 0), this);
			}
			return invalidObject(obj);
		}
		
		public override System.Object convertToObject(Expression expr, Focus focus, EvalContext context)
		{
			XQItem v = expr.evalAsItem(focus, context);
			XQDateTimeBase dt = ((MomentValue) v).Value;
			return new System.DateTime((long) (1000 * dt.GetSecondsFromEpoch()));
		}
	}
}